﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Web;
using System.IO;

namespace NetOpen_System.Component
{
    public class IPCheck
    {
        private string m_LocalIP = HttpContext.Current.Request.UserHostAddress;
        //private string m_LocalIP = "125.122.71.64";
        private bool m_IPIn = false;
        private XmlNode P_XmlNode = null;
        private string s_urlfrom = string.Empty;
        private string s_urlto = string.Empty;
        private string s_outkeyword = string.Empty;

        public IPCheck()
        {

        }

        public IPCheck(XmlNode node)
        {
            P_XmlNode = node;

            if (node == null || !node.HasChildNodes)
                return;
            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.Name == "add")
                {
                    XmlAttributeReader attReader = new XmlAttributeReader(n.Attributes);
                    string ip1 = attReader.GetString("ip1", "");
                    string ip2 = attReader.GetString("ip2", "");
                    if (!IsValidIP(ip1))
                        return;

                    if (!IsValidIP(ip2))
                    {
                        //单IP处理
                        if (m_LocalIP == ip1)
                        {
                            m_IPIn = true;
                            return;
                        }
                    }
                    else if (ip1 == ip2)
                    {
                        //首尾一致的IP段
                        if (m_LocalIP == ip1)
                        {
                            m_IPIn = true;
                            return;
                        }
                    }
                    else
                    {
                        //标准IP段处理
                        if(IsInIp(m_LocalIP,ip1,ip2) == true)
                        {
                            m_IPIn = true;
                            return;
                        }

                    }            
                }
            }
        }

        public bool GetIpIn(string ALocalIp)
        {

            XmlNode node = P_XmlNode;

            if (node == null || !node.HasChildNodes)
                return false;

            bool lb_return = false;


            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.Name == "add")
                {
                    XmlAttributeReader attReader = new XmlAttributeReader(n.Attributes);
                    string ip1 = attReader.GetString("ip1", "");
                    string ip2 = attReader.GetString("ip2", "");
                    if (!IsValidIP(ip1))
                    {
                        lb_return = false;
                    }

                    if (!IsValidIP(ip2))
                    {
                        //单IP处理
                        if (ALocalIp == ip1)
                        {
                            lb_return = true;
                            s_urlfrom = attReader.GetString("urlfrom", "");
                            s_urlto = attReader.GetString("urlto", "");
                            s_outkeyword = attReader.GetString("outkeyword", "");
                            break;
                        }
                    }
                    
                    if (ip1 == ip2)
                    {
                        //首尾一致的IP段
                        if (ALocalIp == ip1)
                        {
                            lb_return = true;
                            s_urlfrom = attReader.GetString("urlfrom", "");
                            s_urlto = attReader.GetString("urlto", "");
                            s_outkeyword = attReader.GetString("outkeyword", "");
                            break;
                        }
                    }
                    else
                    {
                        //标准IP段处理
                        if (IsInIp(ALocalIp, ip1, ip2) == true)
                        {
                            lb_return = true;
                            s_urlfrom = attReader.GetString("urlfrom", "");
                            s_urlto = attReader.GetString("urlto", "");
                            s_outkeyword = attReader.GetString("outkeyword", "");
                            break;
                        }

                    }
                }
            }

            return lb_return;
        }

        /// <summary>
        /// 检测用户ip是否在指定的ip段中
        /// </summary>
        /// <param name="ip">用户ip</param>
        /// <param name="begip">起始ip</param>
        /// <param name="endip">结束ip</param>
        /// <returns></returns>
        public bool IsInIp(string ip, string begip, string endip)
        {
            int[] inip, begipint, endipint = new int[4];
            inip = getIp(ip);
            begipint = getIp(begip);
            endipint = getIp(endip);
            for (int i = 0; i < 4; i++)
            {
                if (inip[i] < begipint[i] || inip[i] > endipint[i])
                {
                    return false;
                }
                else if (inip[i] > begipint[i] || inip[i] < endipint[i])
                {
                    return true;
                }
            }
            return true;
        }

        /// <summary>
        /// 将ip地址转成整形数组
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        protected int[] getIp(string ip)
        {
            int[] retip = new int[4];
            int i, count;
            char c;
            for (i = 0, count = 0; i < ip.Length; i++)
            {
                c = ip[i];
                if (c != '.')
                {
                    retip[count] = retip[count] * 10 + int.Parse(c.ToString());
                }
                else
                {
                    count++;
                }
            }

            return retip;

        }

        private bool IsValidIP(string ip)
        {
            if (string.IsNullOrEmpty(ip))
                return false;
            IPAddress temp;
            return IPAddress.TryParse(ip, out temp);
        }

        /// <summary>
        ///  客户端IP
        /// </summary>
        public string LocalIp
        {
            set { m_LocalIP = value; }
            get { return m_LocalIP; }
        }

        /// <summary>
        /// 判断客户端IP是否在IP列表中
        /// </summary>
        public bool IsIpIn
        {
            get { return m_IPIn; }
        }

        /// <summary>
        /// 需要跳转的原始网页url
        /// </summary>
        public string UrlFrom
        {
            get { return s_urlfrom; }
        }

        /// <summary>
        /// 跳转到目的地网页url
        /// </summary>
        public string UrlTo
        {
            get { return s_urlto; }
        }

        /// <summary>
        /// 排除在外的网址关键字
        /// </summary>
        public string OutKeyWord
        {
            set { s_outkeyword = value; }
            get { return s_outkeyword; }
        }
    }
}
